package com.shhm.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.PageDTO;
import com.shhm.admin.mapper.RoleMapper;
import com.shhm.admin.mapper.UserMapper;
import com.shhm.admin.mapper.UserRoleMapper;
import com.shhm.admin.service.UserRoleService;
import com.shhm.admin.util.RequestUtil;
import com.shhm.common.dto.request.RequestList;
import com.shhm.common.dto.request.admin.SysUserRoleReq;
import com.shhm.common.dto.response.AjaxResultPlus;
import com.shhm.common.dto.response.admin.SysUserRoleVO;
import com.shhm.common.entity.admin.RoleEntity;
import com.shhm.common.entity.admin.SysUser;
import com.shhm.common.entity.admin.SysUserRole;
import com.shhm.common.mapper.SysRoleOperatePermsMapper;
import com.shhm.common.mapper.UserUniversityMapper;
import com.shhm.common.utils.ResponseUtil;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

/**
 * @author JackZ
 * @version 1.0
 * @description: 用户角色服务实现类
 * @date 2025/8/14 下午5:23
 */
@Service
public class UserRoleServiceImpl implements UserRoleService {
    @Resource
    private UserRoleMapper userRoleMapper;
    @Resource
    private SysRoleOperatePermsMapper roleOperatePermsMapper;
    @Resource
    private UserMapper userMapper;
    @Resource
    private UserUniversityMapper userUniversityMapper;
    @Resource
    private RoleMapper roleMapper;

    @Override
    public AjaxResultPlus pageSearchList(Integer pageNum, Integer pageSize, SysUserRoleVO entity) {
        PageDTO<SysUserRoleVO> defPage = new PageDTO<>(pageNum, pageSize);
        return AjaxResultPlus.success(userRoleMapper.pageSearchList(defPage,entity),false);
    }

    @Override
    public AjaxResultPlus pageList(Integer pageNum, Integer pageSize, HttpServletRequest request) {
        PageDTO<SysUserRoleVO> defPage = new PageDTO<>(pageNum, pageSize);
        PageDTO<SysUserRoleVO> list = userRoleMapper.pageList(defPage);
        return AjaxResultPlus.custom(200, false)
                .put("data",list);
    }

    @Override
    public AjaxResultPlus add(SysUserRoleReq entity) {
        // 直接使用传入的userId
        if (Objects.isNull(entity.getUserId()) || entity.getUserId().trim().isEmpty()) {
            return AjaxResultPlus.error("用户ID不能为空");
        }
        
        Long userId = Long.parseLong(entity.getUserId());
        
        // 检查用户是否存在
        SysUser user = userMapper.selectById(userId);
        if (Objects.isNull(user)) {
            return AjaxResultPlus.error("用户不存在");
        }
        
        // 检查角色是否存在
        RoleEntity role = roleMapper.selectById(entity.getRoleId());
        if (Objects.isNull(role)) {
            return AjaxResultPlus.error("角色不存在");
        }
        
        SysUserRole sur = new SysUserRole();
        sur.setRoleId(entity.getRoleId());
        sur.setUserId(userId);
        sur.setCreateTime(LocalDateTime.now());
        return ResponseUtil.sqlExeIsSuccess(userRoleMapper.insert(sur));
    }

    @Override
    public AjaxResultPlus update(SysUserRoleReq entity) {
        // 直接使用传入的userId
        if (Objects.isNull(entity.getUserId()) || entity.getUserId().trim().isEmpty()) {
            return AjaxResultPlus.error("用户ID不能为空");
        }
        
        Long userId = Long.parseLong(entity.getUserId());
        
        // 检查用户是否存在
        SysUser user = userMapper.selectById(userId);
        if (Objects.isNull(user)) {
            return AjaxResultPlus.error("用户不存在");
        }
        
        // 检查角色是否存在
        RoleEntity role = roleMapper.selectById(entity.getRoleId());
        if (Objects.isNull(role)) {
            return AjaxResultPlus.error("角色不存在");
        }
        
        SysUserRole sur = new SysUserRole();
        sur.setRecordId(entity.getRecordId());
        sur.setRoleId(entity.getRoleId());
        sur.setUserId(userId);
        sur.setCreateTime(LocalDateTime.now());
        return ResponseUtil.sqlExeIsSuccess(userRoleMapper.updateById(sur));
    }

    @Override
    public AjaxResultPlus delete(RequestList ids) {
        List<Object> list = ids.getList();
        System.out.println(list.toString());
        return ResponseUtil.sqlExeIsSuccess(userRoleMapper.deleteByIds(list));
    }
}
